package cn.edu.lcu.cs.javaprogramming.thread.sales;

import java.util.Random;

/**
 * 推销员
 *
 * @author ling
 * @date 2022/5/30 16:10
 */
public class Salesman extends Thread {
    /**
     * 余量，余额
     */
    private int balance;

    public Salesman(String name, int balance) {
        super(name);
        this.balance = balance;
    }

    @Override
    public void run() {
        Random random = new Random();
        while (balance > 0) {
            System.out.println(Thread.currentThread().getName() + " 正在推销第 " + balance-- + " 件商品");
            try {
                Thread.sleep(random.nextInt(2000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.err.println(Thread.currentThread().getName() + " 推销完，可以提前下班啦！");
    }
}
